using System;
using System.IO;


namespace mfXmlSerializer
{
    /// <summary>
    /// static container class 4 all common/ shared extension methods
    /// </summary>
    public static class mfCommonExtensions
    {
        #region Extensions
        public static void Check4ChildMap(this mfMapBaseElement element)
        {
            if (!element.HasChildMap)
            {
                throw new mfXmlException("no child map 4 element " + element + " available");
            }
        }

        public static bool IsArrayList(this Type type)
        {
            if (type.FullName.Equals(mfSerializationMapCreator.C_STR_ELEMENT_ARRAYLIST_SERIALIZATION) //direct ArrayList
                 || type.BaseType != null 
                    && type.BaseType.FullName.Equals(mfSerializationMapCreator.C_STR_ELEMENT_ARRAYLIST_SERIALIZATION)) //...or derived of  
            {
                return true;
            }
            return false;
        }

        public static bool IsCustomClass(this Type type)
        {
            if (type.IsClass
                && type.FullName.IndexOf(mfSerializationMapCreator.C_STR_ELEMENT_SERIALIZATION_NAMESPACE) != 0)
            {
                return true;
            }
            return false;
        }

        public static Object CreateInstance(this Type type, Type[] ctorTypes, object[] paraValues)
        {
            return type.GetConstructor(ctorTypes).Invoke(paraValues);
        }

        public static string ConvertMemoryStreamToString(this MemoryStream ms)
        {
            byte[] byteArray = ms.ToArray();
            char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
            return new string(cc);
        }

        public static string ConvertToString(this MemoryStream ms)
        {
            byte[] byteArray = ms.ToArray();
            char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
            return new string(cc);
        }


        #endregion
    }
}
